<template>
    <addEditBaseRoleView ref="addEditBaseRoleView" />
    <addFileView ref="addFileView" />
    <addRoleBasePermission ref="addRoleBasePermission" />
    <setRoleMenu ref="setRoleMenu" />
    <addRolePersonalView ref="addRolePersonalView" />
    <el-container>
        <el-main>
            <div class="teacher">
                <div class="app-table">
                    <div class="app-body">
                        <el-form :inline="true" class="demo-form-inline" v-model="form">
                            <el-form-item label="角色名称">
                                <el-input placeholder="请输入角色名称" v-model="form.name"></el-input>
                            </el-form-item>
                            <!-- <el-form-item label="描述">
                                <el-input placeholder="请输入描述" v-model="form.detail"></el-input>
                            </el-form-item> -->
                            <el-form-item>
                                <el-button class="serach_btn" plain @click="onSubmit"
                                    @keyup.enter="searchEnter">搜索</el-button>
                                <el-button class="add_btn" plain @click="add">添加</el-button>
                                <!-- <el-button type="warning" @click="exportExcel">导出报表</el-button> -->
                                <!-- <el-button type="danger" @click="importExcel">批量导入<el-icon class="el-icon--right"> -->
                                <!-- <Upload /> -->
                                <!-- </el-icon> -->
                                <!-- </el-button> -->
                            </el-form-item>
                        </el-form>
                        <el-table size="default" :data="list" style="width: 100%">
                            <el-table-column prop="id" label="id" />
                            <el-table-column prop="name" label="角色名称" />
                            <el-table-column prop="detail" label="描述" />
                            <!-- <el-table-column prop="isEffect" label="是否可用" /> -->
                            <el-table-column prop="sortNum" label="权重" />
                            <el-table-column label="操作" width="480px">
                                <template style="display: block" #default="scope">
                                    <el-button class="after_menu" size="small" type="warning"
                                        @click="editBaseRole(scope.row, 1)">设置权限
                                    </el-button>
                                    <el-button class="after_menu" size="small" type="warning"
                                        @click="editBaseRole(scope.row, 2)">前台菜单
                                    </el-button>
                                    <el-button class="after_menu" size="small" type="primary"
                                        @click="editBaseMenu(scope.row)">后台菜单
                                    </el-button>
                                    <el-button class="after_menu" size="small" type="primary"
                                        @click="editRolePersonal(scope.row)">角色人员
                                    </el-button>
                                    <el-button class="edit_btn" size="small" type="success"
                                        @click="editBasePermission(scope.row)">编辑
                                    </el-button>
                                    <el-button class="del_btn" size="small" type="danger"
                                        @click="deleteBaseRole(scope.row.id)">删除
                                    </el-button>
                                </template>
                            </el-table-column>
                            <template v-slot:empty>
                                <el-empty description="暂无数据"></el-empty>
                            </template>
                        </el-table>
                        <el-config-provider :locale="locale">
                            <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                                layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                                @current-change="changeCurrentPage">
                            </el-pagination>
                        </el-config-provider>
                    </div>
                    <bottomView />

                </div>
            </div>
        </el-main>
    </el-container>
</template>
<script>
import addRolePersonalView from "@/components/baseInfo/addRolePersonalView.vue";
import addEditBaseRoleView from "@/components/baseInfo/addEditBaseRole.vue";
import setRoleMenu from "@/components/baseInfo/setRoleMenu.vue";
import addRoleBasePermission from "@/components/baseInfo/addRoleBasePermission.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "BaseRoleListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            form: {
                name: "",
                detail: "",
            },
        };
    },
    components: {
        addEditBaseRoleView, addRoleBasePermission,
        addFileView, bottomView, setRoleMenu,
        IconPicture, addRolePersonalView,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getBaseRoleList();
    },
    watch: {
        $route() {
            this.getBaseRoleList();
        },
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getBaseRoleList();
            }
        },
        add() {
            this.$refs.addEditBaseRoleView.title = "添加角色表"
            this.$refs.addEditBaseRoleView.personnelType = this.type
            this.$refs.addEditBaseRoleView.dialogFormVisible = true;
        },
        editBaseRole(obj, type) {
            if (type == 1) {
                this.$refs.addRoleBasePermission.title = "设置权限"
            } else if (type == 2) {
                this.$refs.addRoleBasePermission.title = "设置前台菜单"
            }
            this.$refs.addRoleBasePermission.getBasePermission(obj.id, type);
            this.$refs.addRoleBasePermission.dialogFormVisible = true;
        },
        editBaseMenu(obj) {
            this.$refs.setRoleMenu.title = "设置后台菜单"
            this.$refs.setRoleMenu.getTreeData(obj.id);
            this.$refs.setRoleMenu.dialogFormVisible = true;
        },
        editRolePersonal(obj) {
            this.$refs.addRolePersonalView.title = "角色人员"
            this.$refs.addRolePersonalView.dialogFormVisible = true;
            this.$refs.addRolePersonalView.myRid = obj.id
            this.$refs.addRolePersonalView.getPersonalList(obj.id);
        },
        editBasePermission(obj) {
            this.$refs.addEditBaseRoleView.title = "修改信息"
            this.$refs.addEditBaseRoleView.getBaseRoleById(obj.id);
            this.$refs.addEditBaseRoleView.dialogFormVisible = true;
        },
        deleteBaseRole(id) {
            const arr = [83, 84, 123, 81, 120, 80]
            if (arr.includes(id)) {
                ElMessage.error("该角色禁止删除，请联系管理员！");
                return
            }
            
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/baseRole/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getBaseRoleList();
                            } else {
                                ElMessage.error(data.message);
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getBaseRoleList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入角色表";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/baseRole/importExcel";
        },
        exportExcel() {
            httpService(
                "/javaApi/baseRole/exportExcel",
                {
                    "name": this.form.name,
                    "detail": this.form.detail,
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    window.open(data.msg, '_blank')
                }
            });
        },
        onSubmit() {
            this.getBaseRoleList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getBaseRoleList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getBaseRoleList();
        },
        // tableRowClassName({ row, rowIndex }) {
        //     return "";
        // },
        getBaseRoleList() {
            httpService(
                "/javaApi/baseRole/list/" + this.page + "/" + this.pageSize,
                {
                    "name": this.form.name,
                    "detail": this.form.detail,
                },
                "get"
            ).then((data) => {
                this.list = data.list;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 5px 10px 10px;
}

.app-search {
    margin-top: 20px;
}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 200px);
    text-align: left;

}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}

.between_menu {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.after_menu {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.edit_btn {
    background-color: #fff;
    color: #00c78c;
    border: none;
}

.del_btn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.add_btn {
    background-color: #FF7200;
    color: #fff;
    border-radius: 8px;
    border: none;
}
</style>
